/*
 * marketsim-eclipsetrader
 *
 * Copyright (C) 2008-2011 Steve Phelps and John Cartlidge
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 */
/*
 *  (C) Victria Ltd. 2007
 */
package victria.marketsim.bot;

import org.apache.log4j.Logger;

import victria.marketsim.core.MarketService;
import victria.marketsim.core.Order;
import victria.marketsim.ordertype.OrderType;

public abstract class AbstractBot implements Runnable  {

	protected transient MarketService marketService;

	protected transient BotContainer parent;

	protected String botId;
	
	protected volatile boolean stopped = false;
	
	protected OrderType orderType;
	
	static Logger logger = Logger.getLogger(AbstractBot.class);

	public AbstractBot() {
	}
	
	public void placeOrder(Order order) {
		order.setTrader(botId);
		orderType.setDefaultValues(order);
		logger.info(botId + ": placing order " + order.toPrettyString());
		try {
			marketService.placeOrder(order);
		} catch (Exception e) {
			logger.error(e);
		}
	}
	
	public MarketService getMarketService() {
		return marketService;
	}

	public void setMarketService(MarketService marketService) {
		this.marketService = marketService;
	}

	public BotContainer getParent() {
		return parent;
	}

	public void setParent(BotContainer parent) {
		this.parent = parent;
	}

	public String getBotId() {
		return botId;
	}

	public void setBotId(String botName) {
		this.botId = botName;
	}

	public void run() {
		// TODO Auto-generated method stub
		
	}
	
	public void stop() {
		stopped = true;
	}

	public void setOrderType(OrderType orderType) {
		logger.debug("Setting order type..." + orderType);
		this.orderType = orderType;
	}
	
	public OrderType getOrderType() {
		return orderType;
	}
	
	public boolean equals(Object obj) {
		if (obj instanceof AbstractBot) {
			return this.botId.equals(((AbstractBot) obj).botId);
		} else {
			return false;
		}
	}

	public int hashCode() {
		return botId.hashCode();
	}	
}
